package com.example.service;

//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Student;
import com.example.mapper.StudentMapper;
import com.example.utils.generateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentService {


    //    public boolean saveStudent(Student student){
//
////        return saveOrUpdate(student);
//        if(student.getId() ==null){
//             return save(student);
//        }
//        else {
//            return updateById(student);
//        }
//    }
    @Autowired
    private StudentMapper studentMapper;

//    private List<Integer> inid(Student student)
//    {
//        List<Student> studentid = studentMapper.finAllbyid(student.getId());
//        return extractIds(studentid);
//    }
//    private List<Integer> extractIds(List<Student> studentid) {
//        List<Integer> ids = new ArrayList<>();
//
//        for (Student student : studentid) {
//            int id = student.getId();
//            ids.add(id);
//        }
//        return ids;
//    }
//    public int save(Student student) {
//        if (student.getId() != inid(student).get()) {
//            return studentMapper.insert(student);
//        } else {
//            return studentMapper.update(student);
//        }
//    }
private boolean existsInDb(Student student) {
    List<Student> studentList = studentMapper.findAllByid(student.getStu_num());
    return !studentList.isEmpty();
}
 public int save(Student student) {
        if (existsInDb(student)) {
            return studentMapper.update(student);
        } else {
            return studentMapper.insert(student);
        }
    }


    private boolean existsInDb1(Student student) {
        List<Student> studentList = studentMapper.findAllByid(student.getStu_num());
        return !studentList.isEmpty();
    }
    public int save1(Student student) {
        if (existsInDb1(student)) {
            return studentMapper.update(student);
        } else {
            return studentMapper.insert1(student);
        }
    }
        public Map<String, Object> deleteByStuNum(String stu_num) {
            Integer deleteResult = studentMapper.deleteBy(stu_num);
            if (deleteResult != null) {
                Map<String, Object> data = new HashMap<>();
                data.put("status", "UP22");
                return generateResponse.generateResponse(200, "删除成功", data);
            } else {
                return generateResponse.generateResponse(50000, "删除失败", null);
        }
    }

    public Map<String, Object> saveStudent(Student student) {
        Map<String, Object> result = new HashMap<>();

        Integer saveResult = save1(student);

        if (saveResult != null) {
            result.put("code", 200);
            result.put("message", "保存成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "保存失败");
            result.put("data", null);
        }

        return result;
    }

}
